﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Calendar;
using Google.GData.Extensions;
using Google.GData.Client;
using GenericEntities;

namespace GoogleCalendarHelper
{
    public class CalendarHelper
    {
        public string ApplicationName { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public static CalendarService GetService(string applicationName, string userName, string password)
        {
            CalendarService service = new CalendarService(applicationName);
            service.setUserCredentials(userName, password);
            return service;
        }

        public static IEnumerable<EventEntry> GetAllEvents(CalendarService service, DateTime? startData)
        {
            // Create the query object:
            EventQuery query = new EventQuery();
            query.Uri = new Uri("http://www.google.com/calendar/feeds/" + service.Credentials.Username + "/private/full");
            if (startData != null)
                query.StartTime = startData.Value;

            // Tell the service to query:
            EventFeed calFeed = service.Query(query);
            return calFeed.Entries.Cast<EventEntry>();
        }

        public static void AddEvent(CalendarService service,  string title, string contents, string location, DateTime startTime, DateTime endTime)
        {
            EventEntry entry = new EventEntry();

            // Set the title and content of the entry.
            entry.Title.Text = title;
            entry.Content.Content = contents;

            // Set a location for the event.
            Where eventLocation = new Where();
            eventLocation.ValueString = location;
            entry.Locations.Add(eventLocation);

            When eventTime = new When(startTime, endTime);
            entry.Times.Add(eventTime);

            Uri postUri = new Uri("http://www.google.com/calendar/feeds/default/private/full");

            // Send the request and receive the response:
            AtomEntry insertedEntry = service.Insert(postUri, entry);
        }

        public static void UpdateEvent(CalendarService service, GenericEvent oldGe, GenericEvent newGe)
        {
            var events = GetAllEvents(service, null);

            EventEntry oldEntry = new EventEntry();
            EventEntry newEntry = new EventEntry();

            //오래된 시간 이벤트 엔트리로 초기화
            // Set the title and content of the entry.
            oldEntry.Title.Text = oldGe.Title;
            oldEntry.Content.Content = oldGe.Contents;

            // Set a location for the event.
            Where oldEventLocation = new Where();
            oldEventLocation.ValueString = oldGe.Location;
            oldEntry.Locations.Add(oldEventLocation);

            When oldEventTime = new When(oldGe.StartTime, oldGe.EndTime);
            oldEntry.Times.Add(oldEventTime);

            //수정할 시간 이벤트 엔트리
            newEntry.Title.Text = newGe.Title;
            newEntry.Content.Content = newGe.Contents;

            Where eventLocation = new Where();
            eventLocation.ValueString = newGe.Location;
            newEntry.Locations.Add(eventLocation);

            When eventTime = new When(newGe.StartTime, newGe.EndTime);
            newEntry.Times.Add(eventTime);
            Uri postUri = new Uri("http://www.google.com/calendar/feeds/default/private/full");


            foreach (var eventEntry in events)
            {
                if (eventEntry.Title.Text == oldEntry.Title.Text &&
                    eventEntry.Content.Content == oldEntry.Content.Content &&
                    eventEntry.Locations[0].ValueString == oldEntry.Locations[0].ValueString &&
                    eventEntry.Times[0].StartTime == oldEntry.Times[0].StartTime &&
                    eventEntry.Times[0].EndTime == oldEntry.Times[0].EndTime
                    )

                {
                    if (newGe.Location == "")
                    {
                        service.Delete(eventEntry);
                        service.Insert(postUri, newEntry);
                    }
                    else
                    {
                        eventEntry.Title.Text = newEntry.Title.Text;
                        eventEntry.Content.Content = newEntry.Content.Content;
                        eventEntry.Locations.Clear();
                        eventEntry.Locations.Add(eventLocation);
                        eventEntry.Times.Clear();
                        eventEntry.Times.Add(eventTime);

                        service.Update(eventEntry);
                    }
                    break;
                }

                else if (eventEntry.Title.Text == oldEntry.Title.Text &&
                    eventEntry.Content.Content == oldEntry.Content.Content &&
                    newGe.Location == "" && oldGe.Location == "" &&
                    eventEntry.Times[0].StartTime == oldEntry.Times[0].StartTime &&
                    eventEntry.Times[0].EndTime == oldEntry.Times[0].EndTime
                    )
                {
                    eventEntry.Title.Text = newEntry.Title.Text;
                    eventEntry.Content.Content = newEntry.Content.Content;
                    eventEntry.Times.Clear();
                    eventEntry.Times.Add(eventTime);

                    service.Update(eventEntry);
                    break;
                }

                else if (eventEntry.Title.Text == oldEntry.Title.Text &&
                    eventEntry.Content.Content == oldEntry.Content.Content &&
                    newGe.Location != "" && oldGe.Location == "" &&
                    eventEntry.Times[0].StartTime == oldEntry.Times[0].StartTime &&
                    eventEntry.Times[0].EndTime == oldEntry.Times[0].EndTime
                    )
                {
                    eventEntry.Title.Text = newEntry.Title.Text;
                    eventEntry.Content.Content = newEntry.Content.Content;
                    eventEntry.Locations.Clear();
                    eventEntry.Locations.Add(eventLocation);
                    eventEntry.Times.Clear();
                    eventEntry.Times.Add(eventTime);

                    service.Update(eventEntry);
                    break;
                }
            }
        }

        public static void ClearEvent(CalendarService service, string title, string contents, string location, DateTime startTime, DateTime endTime)
        {
            var events = GetAllEvents(service, null);
            foreach (var eventEntry in events)
            {
                if(eventEntry.Title.Text == title)
                service.Delete(eventEntry);
            }

        }

        public static void ClearAll(CalendarService service)
        {
            var events = GetAllEvents(service, null);
            foreach (var eventEntry in events)
            {
                service.Delete(eventEntry);
            }
        }
    }
}
